﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eniux.Windows.EnterpriseLibrary.Properties;

namespace Eniux.Windows.EnterpriseLibrary.Commands
{
    /// <summary>
    /// 委托命令
    /// </summary>
    /// <typeparam name="T">参数类型</typeparam>
    public class DelegateCommand<T> : DelegateCommandBase
    {
        #region 构造、析构

        public DelegateCommand(Action<T> executeMethod)
            : this(executeMethod, (o) => true)
        {
        }

        public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
            : base((o) => executeMethod((T)o), (o) => canExecuteMethod((T)o))
        {
            //if (executeMethod == null || canExecuteMethod == null)
            //    throw new ArgumentNullException("executeMethod", Resources.DelegateCommandDelegatesCannotBeNull);

            #if !WINDOWS_PHONE
            Type genericType = typeof(T);

            // DelegateCommand allows object or Nullable<>.  
            // note: Nullable<> is a struct so we cannot use a class constraint.
            if (genericType.IsValueType)
            {
                if ((!genericType.IsGenericType) || (!typeof(Nullable<>).IsAssignableFrom(genericType.GetGenericTypeDefinition())))
                {
                    // throw new InvalidCastException(Resources.DelegateCommandInvalidGenericPayloadType);
                }
            }
            #endif
        } 

        #endregion

        #region 方法

        public bool CanExecute(T parameter)
        {
            return base.CanExecute(parameter);
        }

        public void Execute(T parameter)
        {
            base.Execute(parameter);
        } 

        #endregion
    }

    /// <summary>
    /// 委托命令
    /// </summary>
    public class DelegateCommand : DelegateCommandBase
    {
        #region 构造、析构

        public DelegateCommand(Action executeMethod)
            : this(executeMethod, () => true)
        {
        }

        public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod)
            : base((o) => executeMethod(), (o) => canExecuteMethod())
        {
            //if (executeMethod == null || canExecuteMethod == null)
            //    throw new ArgumentNullException("executeMethod", Resources.DelegateCommandDelegatesCannotBeNull);
        }
        
        #endregion

        #region 方法

        public void Execute()
        {
            Execute(null);
        }

        public bool CanExecute()
        {
            return CanExecute(null);
        } 

        #endregion
    }
}
